package com.dyz.leetcode._12_linkedList;

import com.dyz.leetcode.ListNode;

import java.util.HashSet;
import java.util.Set;

public class _141_hasCycle {
    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        while (head != null) {
            if (set.contains(head)) {return true;}
            set.add(head);
            head = head.next;
        }
        return false;
    }
    //快慢指针
    public boolean hasCycle1(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (fast!=null && fast.next!=null){
            fast = fast.next.next;
            slow = slow.next;
            if(fast==slow){return true;}
        }
        return false;
    }
}
